package org.opsli.modulars.mini;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.opsli.common.exception.ServiceException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Configuration
@EnableConfigurationProperties({WxMpLoginProperties.class,WxMpPayProperties.class})
public class WxMpConfig {

    private final WxMpLoginProperties loginProperties;
    private final WxMpPayProperties payProperties;
    public WxMpConfig(WxMpLoginProperties loginProperties, WxMpPayProperties payProperties) {
        this.loginProperties = loginProperties;
        this.payProperties = payProperties;
    }

    /**
     * 与微信小程序有关的总service
     * @return WxMaService
     */
    @Bean
    public WxMaService wxMaService() {
        List<WxMpLoginProperties.Config> configs = this.loginProperties.getConfigs();
        if (configs == null) {
            throw new ServiceException(500,"没有微信小程序配置啊");
        }
        WxMaService wxService = new WxMaServiceImpl();
        Map<String, WxMaConfig> multiConfigs = configs.stream()
                .map(cof -> {
                    WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
                    config.setAppid(cof.getAppid());
                    config.setSecret(cof.getSecret());
                    config.setToken(cof.getToken());
                    config.setAesKey(cof.getAesKey());
                    config.setMsgDataFormat(cof.getMsgDataFormat());
                    return config;
                })
                .collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, Function.identity(), (o, n) -> o));
        wxService.setMultiConfigs(multiConfigs);
        return wxService;
    }

    @Bean
    public WxPayService wxPayService() {
        List<WxMpPayProperties.Config> configs = this.payProperties.getConfigs();
        if (configs == null) {

            throw new ServiceException(500,"没有微信小程序配置啊");
        }
        WxPayService wxPayService = new WxPayServiceImpl();
//        this.payProperties.getConfigs().forEach(merchant -> {
//            WxPayConfig payConfig = new WxPayConfig();
//            payConfig.setMchId(merchant.getMchId());
//            payConfig.setCertSerialNo(merchant.getMchSerialNo());
//            payConfig.setApiV3Key(merchant.getWxApiV3Key());
//            payConfig.setPrivateKeyContent(merchant.getApiClientKey().getBytes());
//            payConfig.setPrivateCertContent(merchant.getApiClientCert().getBytes());
//            wxPayService.addConfig(merchant.getMchId(), payConfig);
//            // 可以指定是否使用沙箱环境
//            payConfig.setUseSandboxEnv(false);
//        });
        return wxPayService;
    }

}
